/*
 * 
 * Libman
 * 
 * Daniel Kaufmann, Martin Lang, Simon Moosbrugger,
 * Stefan Mayer, Stephan Halbeisen
 * 
 */
package model.dao;

import hibernate.HibernateUtil;

import java.util.List;

import model.Keyword;
import model.Loan;
import model.User;

import org.hibernate.Session;
import org.hibernate.criterion.Disjunction;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;

import application.exceptions.UserNotInDbException;

/**
 * A DAO implementation for Loan objects.
 */

public class UserDAO extends GenericHibernateDAO<User, Long> {
	public User getUser(String username) throws UserNotInDbException {
		Session session = HibernateUtil.currentSession();
		Disjunction crit = Restrictions.disjunction();

		crit.add(Restrictions.like("username", username, MatchMode.EXACT));

		@SuppressWarnings("unchecked")
		List<User> userList = session.createCriteria(User.class)
				.add(crit).list();

		if(userList.size() == 0){
			//User has to be in the db of libman
			throw new UserNotInDbException();
		}
		
		return userList.get(0);
	}
	
	public User getUserBySession(String session){
		Session s = HibernateUtil.currentSession();
		Disjunction crit = Restrictions.disjunction();

		crit.add(Restrictions.like("session", session, MatchMode.EXACT));

		@SuppressWarnings("unchecked")
		List<User> userList = s.createCriteria(User.class)
				.add(crit).list();

		if(userList.size() == 0){
			//Exception
		} else if(userList.size() > 1){
			//Exception
		}
		
		return userList.get(0);
	}
}